package sitemapSeed;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class SiteMapSeeder {

	public void produceMap() {
		System.setProperty("LOG_DIR", "./");
		Log logger = LogFactory.getLog(SiteMapSeeder.class);
		Document document = new Document();
		// 创建根元素urlset
		Element urlset = new Element("urlset");
		document.addContent(urlset);
		Namespace ns = Namespace.getNamespace("http://www.sitemaps.org/schemas/sitemap/0.9");
		urlset.setNamespace(ns);
		/*Attribute urlset_xmlns = new Attribute("xmlns2", "http://www.sitemaps.org/schemas/sitemap/0.9");
		urlset.setAttribute(urlset_xmlns);*/
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		// 如果文件存在

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbName + "?user=root&password=777888999a!&useUnicode=true&characterEncoding=utf8");
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select id from " + dbTableName);
			while (rs.next()) {
				// 创建父元素url
				Element url = new Element("url");
				url.setNamespace(ns);
				urlset.addContent(url);

				Element loc = new Element("loc");
				loc.setNamespace(ns);
				loc.setText(URL + rs.getInt("id") + ".html");
				url.addContent(loc);

				Element lastmod = new Element("lastmod");
				lastmod.setNamespace(ns);
				lastmod.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
				url.addContent(lastmod);

				Element changefreq = new Element("changefreq");
				changefreq.setNamespace(ns);
				changefreq.setText("monthly");
				url.addContent(changefreq);

				Element priority = new Element("priority");
				priority.setNamespace(ns);
				priority.setText("0.8");
				url.addContent(priority);

				logger.info(rs.getInt(1));
			}
			
			// 设置xml输出格式
			
			Format format = Format.getPrettyFormat();
			format.setEncoding("utf-8");// 设置编码
			format.setIndent("    ");// 设置缩进
			// 得到xml输出流
			XMLOutputter out = new XMLOutputter(format);
			// 把数据输出到xml中
			out.output(document, new FileOutputStream(sitemapName));// 或者FileWriter
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 如果文件不存在
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void setHost(String host) {
		this.host = host;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public void setDbTableName(String dbTableName) {
		this.dbTableName = dbTableName;
	}

	public void setSitemapName(String sitemapName) {
		this.sitemapName = sitemapName;
	}

	public void setURL(String uRL) {
		URL = uRL;
	}

	public void setBeginner(int beginner) {
		this.beginner = beginner;
	}

	String host = null;
	String dbName = null;
	String dbTableName = null;
	String sitemapName = null;
	String URL = null;
	int beginner = 0;
}
